package com.yu.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TtlQueueConfig {

    private static final String EXCHANGES_X = "X";//普通交换机
    private static final String QUEUE_A = "QA";//普通队列
    private static final String QUEUE_B = "QB";//普通队列

    private static final String DEAD_EXCHANGES_Y = "Y";//死信交换机
    private static final String DEAD_QUEUE = "QD";//死信队列


    @Bean("exchangeX") //声明普通交换机
    public DirectExchange Exchange_X(){
        return new DirectExchange(EXCHANGES_X);
    }

    @Bean("queueA")//声明A队列
    public Queue QUEUE_A(){
        Map<String,Object> args = new HashMap<>();
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_EXCHANGES_Y);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的 TTL
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(args).build();
    }
    @Bean("queueB")//声明B队列
    public Queue QUEUE_B(){
        Map<String,Object> args = new HashMap<>();
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_EXCHANGES_Y);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的 TTL
        args.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(args).build();
    }

    @Bean //绑定A队列和X交换机
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("exchangeX") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    @Bean//绑定B队列和X交换机
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("exchangeX") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }

    @Bean("exchangeY")//声明死信交换机
    public DirectExchange DEAD_EXCHANGES_Y(){
        return new DirectExchange(DEAD_EXCHANGES_Y);
    }

    @Bean("queueD")
    public Queue QUEUE_D(){
        return new Queue(DEAD_QUEUE);
    }

    @Bean//绑定D队列和Y交换机
    public Binding queueDBindingX(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("exchangeY") DirectExchange xExchange){
        return BindingBuilder.bind(queueD).to(xExchange).with("YD");
    }

    //由于上面定义两个队列，一个10s 1个40s，但是现在没有动态的过期时间，所以我们定义了C队列，可以让
    //动态设置时间
    @Bean("queueC")//声明C队列 和死信交换机
    public Queue QUEUE_C(){
        Map<String,Object> args = new HashMap<>();
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_EXCHANGES_Y);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable("QC").withArguments(args).build();
    }

    @Bean //绑定C队列和X交换机
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("exchangeX") DirectExchange xExchange){
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }

}
